home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / util / libs / regex.lha / regex / examples / k.c
Encoding:
C/C++ Source or Header  |  1999-04-23  |  1.4 KB  |  78 lines

  1.  
  2. #include <proto/regex.h>
  3. #include <proto/exec.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #define NS 10
  8.  
  9. struct Library *RegexBase;
  10.  
  11. void main(int argc,char **argv)
  12. {
  13.     regmatch_t     subs[NS];
  14.     ULONG         eopts=0;
  15.     int         i, len;
  16.     regoff_t     startoff = 0;
  17.     regoff_t     endoff = 0;
  18.     char errbuf[256];
  19.     regex_t    re;
  20.     int copts = REG_EXTENDED;
  21.     int err;
  22.  
  23.     if (argc!=3)
  24.     {
  25.         printf("Usage: k <regex> <string>\n");
  26.         return;
  27.     }
  28.  
  29.     RegexBase = OpenLibrary("regex.library",0);
  30.     if (!RegexBase)
  31.     {
  32.         printf("no RegexBase\n");
  33.         return;
  34.     }
  35.  
  36.     err = regcomp(&re,argv[1],copts);
  37.     if (err)
  38.     {
  39.         regerror(err,&re,errbuf,sizeof(errbuf));
  40.         printf("regcomp: %s\n",errbuf);
  41.         CloseLibrary(RegexBase);
  42.         return;
  43.     }
  44.  
  45.     subs[0].rm_so = startoff;
  46.     subs[0].rm_eo = strlen(argv[2]) - endoff;
  47.  
  48.     err = regexec(&re,argv[2],(size_t)NS,subs,eopts);
  49.     if (err)
  50.     {
  51.         regerror(err,&re,errbuf,sizeof(errbuf));
  52.         printf("regexec: %s\n",errbuf);
  53.         regfree(&re);
  54.         CloseLibrary(RegexBase);
  55.         return;
  56.     }
  57.  
  58.     len = (int)(subs[0].rm_eo - subs[0].rm_so);
  59.     if (subs[0].rm_so != -1)
  60.     {
  61.         printf("len:%d   ",len);
  62.         if (len != 0)
  63.                 printf("<match `%.*s'\n", (int)len,
  64.                     argv[2] + subs[0].rm_so);
  65.             else
  66.                 printf(">match `'@%.1s\n",
  67.                     argv[2] + subs[0].rm_so);
  68.     }
  69.     for (i = 1; i < NS; i++)
  70.     {
  71.         if (subs[i].rm_so != -1)
  72.             printf("(%ld) %ld `%.*s'\n",i,(int)(subs[i].rm_eo - subs[i].rm_so),argv[2] + subs[i].rm_so);
  73.     }
  74.  
  75.     regfree(&re);
  76.     CloseLibrary(RegexBase);
  77. }
  78.